package com.dominator.serviceImpl;

import com.dominFramework.core.id.SystemIdUtils;
import com.dominFramework.core.typewrap.Dto;
import com.dominFramework.core.utils.SystemUtils;
import com.dominator.enums.ReqEnums;
import com.dominator.mybatis.dao.*;
import com.dominator.repository.DaoAccount;
import com.dominator.repository.DaoOrder;
import com.dominator.repository.DaoUser;
import com.dominator.service.*;
import com.dominator.utils.api.ApiMessage;
import com.dominator.utils.api.ApiMessageUtil;
import com.dominator.utils.api.ApiUtils;
import com.dominator.utils.exception.ApiException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class ApiOrderServiceImpl implements ApiOrderService {

    @Autowired
    DaoOrder orderDao;

    @Autowired
    DaoUser userDao;

    @Autowired
    DaoAccount accountDao;

    @Autowired
    T_order_typeDao tOrderTypeDao;

    @Autowired
    T_userDao tUserDao;
    @Autowired
    T_game_priceDao tGamePriceDao;

    @Autowired
    ApiUserService apiUserService;
    @Autowired
    ApiAccountService apiAccountService;

    @Autowired
    ApiEntranceTicketService apiEntranceTicketService;
    @Autowired
    ApiGameOrderService apiGameOrderService;

    @Override
    @Transactional(propagation= Propagation.REQUIRED)
    public ApiMessage createOrder(Dto dto) throws ApiException {
        ApiUtils.checkParam(dto, "typeId");
        T_userPO user = apiUserService.getUserByDto(dto);
        T_accountPO account=apiAccountService.getAccountByUserId(user.getId());
        T_order_typePO tOrderTypePO =tOrderTypeDao.selectByKey(dto.getString("typeId"));
        if (SystemUtils.isEmpty(tOrderTypePO)){
            throw new ApiException(ReqEnums.ORDERTYPE_IS_NOT_EXSIT);
        }

        dto.put("userId", user.getId());
        dto.put("accountId", account.getId());
        dto.put("title",tOrderTypePO.getType_name());

        switch (dto.getInteger("typeId")) {
            case 1:
                //福分兑入
                break;

            case 2:
                //福分消费
                break;

            case 3:
                //福分核销

                break;
            case 4:
                //福分充值
                if (SystemUtils.isEmpty(dto.getBigDecimal("totalAmount"))){
                    throw  new ApiException(ReqEnums.ACC_IS_NULL);
                }
                dto.put("points",dto.getBigDecimal("totalAmount").multiply(BigDecimal.TEN));
                orderDao.pointsRecharge(dto);
                break;
            case 5:
                //福分购彩
                if (SystemUtils.isEmpty(dto.getBigDecimal("points"))){
                    throw  new ApiException(ReqEnums.POINTS_IS_NULL);
                }
                orderDao.deduPoints(dto);
                apiAccountService.deductPoints(dto);
                break;
            case 6:
                //余额购彩
                if (SystemUtils.isEmpty(dto.getBigDecimal("points"))){
                    throw  new ApiException(ReqEnums.ACC_IS_NULL);
                }
                orderDao.deduMoney(dto);
                apiAccountService.deductPoints(dto);
                //todo 组合支付
                break;
            case 7:
                //福分退回
                if (SystemUtils.isEmpty(dto.getBigDecimal("point"))){
                    throw  new ApiException(ReqEnums.ACC_IS_NULL);
                }
                orderDao.addPoints(dto);
                apiAccountService.addPoints(dto);
                break;
            case 8:
                //奖金返奖
                if (SystemUtils.isEmpty(dto.getBigDecimal("money"))){
                    throw  new ApiException(ReqEnums.ACC_IS_NULL);
                }
                orderDao.addMoney(dto);
                //apiAccountService.addMoney(dto);
                break;
            case 9:
                //奖金提现
                orderDao.withdraw(dto);
                apiAccountService.withdraw(dto);
                break;


            case 10:
                //会员升级
                if (SystemUtils.isEmpty(dto.getInteger("upgrade"))){
                    throw  new ApiException(ReqEnums.UPGRADE_IS_NULL);
                }
                String memberGrade = SystemUtils.isEmpty(user.getMember_grade())?"1":user.getMember_grade();
                Integer grade=Integer.valueOf(memberGrade);
                Integer upgrades=dto.getInteger("upgrade")-grade;
                BigDecimal points=new BigDecimal(500).multiply(new BigDecimal(upgrades));
                dto.put("points",points);
                orderDao.upgradeAddPoints(dto);
                break;
            case 11:
                //会员续费
                if (SystemUtils.isEmpty(dto.getInteger("upgrade"))){
                    throw new ApiException(ReqEnums.UPGRADE_IS_NULL);
                }
                if ("2".equals(user.getMember_grade())){
                    dto.put("points",new BigDecimal(500));
                }
                if ("3".equals(user.getMember_grade())){
                    dto.put("points",new BigDecimal(1000));
                }
                orderDao.upgradeAddPoints(dto);
                break;

            case 12:
                //余额消费
                if (SystemUtils.isEmpty(dto.getInteger("consumptionAmount"))){
                    throw new ApiException(ReqEnums.CONSUMPTION_AMOUNT_IS_NULL);
                }
                orderDao.consumption(dto);
                break;
            case 13:
                //会员升级补差价，不加时间
                if (SystemUtils.isEmpty(dto.getInteger("upgrade"))){
                    throw  new ApiException(ReqEnums.UPGRADE_IS_NULL);
                }
                String memberGrade2 = SystemUtils.isEmpty(user.getMember_grade())?"1":user.getMember_grade();
                Integer grade2=Integer.valueOf(memberGrade2);
                Integer upgrades2=dto.getInteger("upgrade")-grade2;
                BigDecimal points2=new BigDecimal(500).multiply(new BigDecimal(upgrades2));
                dto.put("points",points2);
                orderDao.upgradeAddPoints(dto);
                break;
            case 14:
                //积分充值，买门票
                if (SystemUtils.isEmpty(dto.getBigDecimal("totalAmount"))){
                    throw  new ApiException(ReqEnums.ACC_IS_NULL);
                }
                dto.put("points",dto.getBigDecimal("totalAmount").multiply(BigDecimal.TEN));
                orderDao.pointsRecharge(dto);
                apiEntranceTicketService.createTicket(dto);

                break;
            case 15:
                //余额玩游戏
                ApiUtils.checkParam(dto,"prizeId");
                T_game_pricePO tGamePricePO = tGamePriceDao.selectByKey(dto.getString("prizeId"));
                if (SystemUtils.isNotEmpty(tGamePricePO)){
                    dto.put("money",tGamePricePO.getMoney());
                }
                apiAccountService.deductMoney(dto);
                orderDao.deduMoney(dto);
                //添加游戏订单记录等待刮票
                dto.put("prizeId",tGamePricePO.getId());
                return apiGameOrderService.addGameOrder(dto);

            case 18:
                //微信在线充值
                if (SystemUtils.isEmpty(dto.getBigDecimal("totalAmount"))){
                    throw  new ApiException(ReqEnums.ACC_IS_NULL);
                }
                orderDao.WXRechargeMoney(dto);

        }
        return ApiMessageUtil.success();

    }



    @Override
    public ApiMessage getOrderNotice(Dto dto) throws ApiException {
        List<Dto> res=orderDao.getNoticeList(dto);
        return ApiMessageUtil.success(res);
    }

    @Override
    public ApiMessage getGameOrderNotice(Dto dto) throws ApiException {
        ApiUtils.checkParam(dto, "userId");
        dto.setPageLimit(dto.getInteger("pageLimit"));
        dto.setPageStart(dto.getInteger("pageStart"));
        return ApiMessageUtil.success(orderDao.getGameNoticeListPage(dto));
    }

    public T_userPO getUserById(String userId) throws ApiException {
        T_userPO tUserPO = tUserDao.selectByKey(userId);
        if (SystemUtils.isEmpty(tUserPO)) {
            throw new ApiException(ReqEnums.REQ_USER_NOT_EXSIT);
        } else if ("0".equals(tUserPO.getIs_valid())) {
            throw new ApiException(ReqEnums.REQ_USER_IS_NOT_VALID);
        }
        return tUserPO;
    }
}
